import Vue from 'vue'
import VueRouter from 'vue-router'
// 通过路由懒加载的方式优化路由组件
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue');
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue');
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue');

// import Users from '../components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue');
// import Rights from '../components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue');
// import Roles from '../components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue');

// import Goods from '../components/goods/Goods.vue'
const Goods = () => import(/* webpackChunkName: "Goods_Params_Categories_AddGoods" */ '../components/goods/Goods.vue');
// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "Goods_Params_Categories_AddGoods" */ '../components/goods/Params.vue');
// import Categories from '../components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "Goods_Params_Categories_AddGoods" */ '../components/goods/Categories.vue');
// import AddGoods from '../components/goods/AddGoods.vue'
const AddGoods = () => import(/* webpackChunkName: "Goods_Params_Categories_AddGoods" */ '../components/goods/AddGoods.vue');

// import Orders from '../components/order/Orders.vue'
const Orders = () => import(/* webpackChunkName: "Orders_Reports" */ '../components/order/Orders.vue');
// import Reports from '../components/report/Reports.vue'
const Reports = () => import(/* webpackChunkName: "Orders_Reports" */ '../components/report/Reports.vue');

Vue.use(VueRouter);

const routes = [
  // 根组件重定向到登录界面
  { path: '/', redirect: '/login' },
  // 登录路由
  { path: '/login', component: Login },
  // 主页路由
  {
    path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/goods', component: Goods },
      { path: '/params', component: Params },
      { path: '/categories', component: Categories },
      { path: '/goods/add', component: AddGoods },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
});

// 挂载路由导航守卫功能
router.beforeEach((to, from, next) => {
  // to 表示 将要访问的路径
  // from 表示从哪个路径跳过来的
  // next() 表示放行  next('/login') 表示强制跳转
  if (to.path === '/login') {
    return next();
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login')
  // console.log(Vue.prototype.$axios);
  next();
});

export default router;
